package com.todo.biz.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.biz.infrastructure.entity.condition.ExamineRecordCondition;
import com.todo.biz.infrastructure.entity.po.BizExamineRecordPO;
import com.todo.biz.infrastructure.mapper.BizExamineRecordMapper;
import com.todo.biz.infrastructure.repository.BizExamineRecordRepository;
import lombok.extern.slf4j.Slf4j;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (BizExamineRecord)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:03
 */
@Slf4j
@Repository("bizExamineRecordRepository")
public class BizExamineRecordRepositoryImpl implements BizExamineRecordRepository {

    @Resource
    private BizExamineRecordMapper bizExamineRecordMapper;


    @Override
    public PageResult<BizExamineRecordPO> queryBizExamineRecordByCondition(ExamineRecordCondition examineRecordCondition) {
        return null;
    }

    @Override
    public BizExamineRecordPO queryBizExamineRecordPOById(Long id) {
        if (id == null || id <= 0) {
            return null;
        }
        return bizExamineRecordMapper.queryBizExamineRecordPOById(id);
    }

    @Override
    public List<BizExamineRecordPO> queryBizExamineRecordPOByBusinessId(Long businessId) {
        if (businessId == null || businessId <= 0) {
            return Lists.newArrayList();
        }
        return bizExamineRecordMapper.queryBizExamineRecordPOByBusinessId(businessId);
    }

    @Override
    public Boolean insertBizExamineRecordPO(BizExamineRecordPO bizExamineRecordPO) {
        if (bizExamineRecordPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!baseFieldCheck(bizExamineRecordPO)) {
            return false;
        }
        return bizExamineRecordMapper.insertBizExamineRecordPO(bizExamineRecordPO) > 0;
    }


    @Override
    public Boolean updateBizExamineRecordPOById(BizExamineRecordPO bizExamineRecordPO) {
        if (bizExamineRecordPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!baseFieldCheck(bizExamineRecordPO)) {
            return false;
        }
        return bizExamineRecordMapper.updateBizExamineRecordPOById(bizExamineRecordPO) > 0;
    }

    @Override
    public Boolean removeBizExamineRecordPOById(Long id) {
        if (id == null || id <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        BizExamineRecordPO bizExamineRecordPO = queryBizExamineRecordPOById(id);
        if (bizExamineRecordPO == null) {
            return true;
        }
        return bizExamineRecordMapper.removeBizExamineRecordPOById(id) > 0;
    }

    private boolean baseFieldCheck(BizExamineRecordPO bizExamineRecordPO) {
        if (bizExamineRecordPO == null) {
            return false;
        }
        if (bizExamineRecordPO.getExamineType() == null || bizExamineRecordPO.getBusinessScene() == null || bizExamineRecordPO.getExamineStatus() == null) {
            log.error("BizExamineRecordRepository#baseFieldCheck 基础字段校验失败，类型数据缺失：【{}】", JSONObject.toJSONString(bizExamineRecordPO));
            return false;
        }
        if (bizExamineRecordPO.getBusinessId() == null || bizExamineRecordPO.getSourceInfo() == null) {
            log.error("BizExamineRecordRepository#baseFieldCheck 基础字段校验失败，业务数据缺失：【{}】", JSONObject.toJSONString(bizExamineRecordPO));
            return false;
        }
        return true;
    }
}
